<template>
	<u-popup mode="bottom" :value="value" border-radius="20" @close="handleClose">
		<view class="k-popup">
			<view class="k-popup-head">
				<u-image :src="logo" width="100" height="100" border-radius="50%"></u-image>
				<text class="app-name">{{appName}}</text>
			</view>
			<view class="k-popup-des">
				建议使用您的微信头像和昵称，以便获得更好的体验
			</view>
			<view class="k-popup-body">
				<u-form :model="form" ref="userInfoForm" :rules="rules">
					<u-form-item label="头像" :required="true" prop="avatar">
						<button open-type="chooseAvatar" class="choose-avatar" @chooseavatar="handleChooseAvatar">
							<view class="inputAvatar">
								<u-input type="select" v-model="form.avatar"></u-input>
							</view>
							<u-icon name="plus" size="42" color="#909399"></u-icon>
							<image :src="form.avatar" class="avatar-image" mode="aspectFill"></image>
							<!-- <text>添加图片</text> -->
						</button>
					</u-form-item>
					<u-form-item label="昵称" :required="true" prop="nickname">
						<u-input v-model="form.nickname" type="nickname" @blur="handleBlur" placeholder="请输入昵称"/>
					</u-form-item>
					
				</u-form>
				<view class="k-popup-foot">
					<u-button type="primary" :ripple="true" @click="submit">确定</u-button>
					<view class="cancel">
						<u-button :plain="true" :ripple="true" @click="handleClose">暂不登录</u-button>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		name: "k-get-user-info-popup",
		props: {
			value: {
				type: Boolean,
				required: true
			},
			logo: {
				type: String,
				default: "/static/logo.png"
			},
			appName: {
				type: String,
				default: "可达鸭"
			},
			des: {
				type: String,
				default: "建议使用您的微信头像和昵称，以便获得更好的体验"
			}
		},
		data() {
			return {
				form: {
					avatar: "",
					nickname: "",
				},
				rules: {
					avatar: [
						// 必填规则
						{
							required: true,
							message: '请选择头像',
							// blur和change事件触发检验
							trigger: ['blur', 'change'],
						},

					],
					nickname: [
						// 正则判断为字母或数字
						{
							required: true,
							message: '请输入昵称',
							trigger: ['blur', 'change'],
						},
					]
				}
			};
		},
		created() {

		},
		mounted() {
			// #ifdef MP-WEIXIN
			this.$refs.userInfoForm.setRules(this.rules);
			// #endif
		},
		methods: {
			handleBlur(nickname){
				this.form.nickname = nickname
			},
			handleChooseAvatar(e){
				this.form.avatar = e.detail.avatarUrl
			},
			handleClose(){
				this.close();
				this.$emit('close');
			},
			close(){
				this.$emit('input',false)
			},
			submit() {
				this.$refs.userInfoForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						this.$emit("getUserInfo",this.form)
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.k-popup {
		padding: 30rpx;

		&-head {
			display: flex;
			align-items: center;

			.app-name {
				margin-left: 10rpx;
				color: $u-main-color;
				font-size: 32rpx;
				font-weight: 700;
			}
		}

		&-des {
			margin-top: 30rpx;
			color: $u-type-info;
			font-size: 28rpx;
		}

		&-body {
			padding: 0 15rpx;
			margin-top: 50rpx;

			.choose-avatar {
				position: relative;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 150rpx;
				height: 150rpx;
				background-color: transparent;
				padding: 0;
				margin: 0;
				line-height: normal;
				border: 1px solid $u-border-color;
				// color: $u-type-info;
				// font-size: 30rpx;
				.avatar-image {
					position: absolute;
					width: 100%;
					height: 100%;
				}
				.inputAvatar{
					position: absolute;
					width: 100%;
					height: 100%;
					z-index: 2;
					opacity: 0;
				}
			}
		}
		&-foot{
			margin-top: 50rpx;
			.cancel{
				margin-top: 30rpx;
			}
		}
	}
</style>